package com.project.web.system.controller.app;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.project.common.core.utils.exception.Result;
import com.project.common.core.utils.id.IDUtil;
import com.project.web.system.controller.common.BaseController;
import com.project.web.system.model.app.StdCode;
import com.project.web.system.model.app.StdCodeType;
import com.project.web.system.service.app.StdCodeService;
import com.project.web.system.service.app.StdCodeTypeService;
import com.project.web.util.exception.SYSTEM_RESP_CODE_ENUM;
import com.project.web.util.exception.SystemCustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @ClassName StdCodeTypeController
 * @Description
 * @Author WYY
 * @Date 2020/8/21 4:40 PM
 * @Version 1.0
 */
@Api(tags = "【Web】标准编码控制器")
@RestController("stdCodeTypeController")
@RequestMapping("/system/auth/std/code/type")
public class StdCodeTypeController extends BaseController {

    Logger logger = LoggerFactory.getLogger(StdCodeTypeController.class);

    @Autowired
    private StdCodeTypeService stdCodeTypeService;

    @Autowired
    private StdCodeService stdCodeService;

    @ApiOperation(value = "分页查询标准编码类型信息")
    @GetMapping(value = "/selectList")
    public Result selectList(StdCodeType stdCodeType, Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }

        Page page = new Page();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        PageInfo<StdCodeType> stdCodeTypePageInfo = stdCodeTypeService.selectList(stdCodeType, page);
        return getResult(stdCodeTypePageInfo);
    }

    @ApiOperation(value = "标准编码类型保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "StdCodeType", value = "StdCodeType", dataType = "StdCodeType")
    })
    @PostMapping("/save")
    public Result save(@RequestBody StdCodeType stdCodeType) {
        if (stdCodeType == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        stdCodeType.setId(IDUtil.getId());
        stdCodeType.setCreateDate(new Date());


        stdCodeTypeService.save(stdCodeType);
        return getResult("保存成功");
    }


    @ApiOperation(value = "标准编码类型修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "StdCodeType", value = "StdCodeType", dataType = "StdCodeType")
    })
    @PostMapping("/edit")
    public Result edit(@RequestBody StdCodeType stdCodeType) {
        if (stdCodeType == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        stdCodeType.setModifyDate(new Date());
        stdCodeTypeService.update(stdCodeType);
        return getResult("修改成功");
    }

    @ApiOperation(value = "删除编码表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "Integer")
    })
    @GetMapping("/code/delete")
    public Result delete(Long id) {
        if (id == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        stdCodeService.delete(id);
        return getResult("修改成功");
    }


    @ApiOperation(value = "查询标准编码信息")
    @GetMapping(value = "/getByCodeTypeId")
    public Result getByCodeTypeId(Long id) {
        if (id == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        StdCode stdCode = new StdCode();
        stdCode.setCodeTypeId(id);
        return getResult(stdCodeService.selectList(stdCode));
    }

    @ApiOperation(value = "标准编码保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "StdCode", value = "StdCode", dataType = "StdCode")
    })
    @PostMapping("/code/save")
    public Result save(@RequestBody StdCode stdCode) {
        if (stdCode == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        stdCode.setCreateDate(new Date());
        stdCode.setModifyDate(new Date());
        stdCode.setId(IDUtil.getId());
        stdCodeService.save(stdCode);
        return getResult("保存成功");
    }

    @ApiOperation(value = "标准编码类型修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "StdCode", value = "StdCode", dataType = "StdCode")
    })
    @PostMapping("/code/edit")
    public Result edit(@RequestBody StdCode stdCode) {
        if (stdCode == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        stdCode.setModifyDate(new Date());
        stdCodeService.update(stdCode);
        return getResult("修改成功");
    }
}
